package com.dingli.framework.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author 密码加密
 */
public class Encryption {

    public static String md5(String plainText) {
        if (null == plainText) {
            plainText = "";
        }
        String md5Str = "";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            byte[] b = md.digest();

            int i;

            StringBuilder builder = new StringBuilder(32);
            for (byte value : b) {
                i = value;
                if (i < 0) {
                    i += 256;
                }
                if (i < 16) {
                    builder.append("0");
                }
                builder.append(Integer.toHexString(i));
            }
            md5Str = builder.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return md5Str;
    }

    public static void main(String[] args) {
        System.out.println("md5(\"Aa5556.\") = " + md5("Aa55555."));
        //88c41a006c1d71cea4ea33c1096317dc
        //88c41a006c1d71cea4ea33c1096317dc
        System.out.println("md5(\"Aa5556.\") = " + md5("Aa5556."));
    }

}
